using System;
using System.Collections.Generic;
using System.Text;
using WCSFContrib.Extensions.CompositeWeb.Services;
using WCSFContrib.Extensions.CompositeWeb.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.Extensions.CompositeWeb.Interfaces;

namespace WCSFContrib.Extensions.CompositeWeb.Tests.Services
{
    [TestClass]
    public class WebConfigModuleInfoStoreFixture
    {
        [TestMethod]
        [DeploymentItem(@"Services\Support\OneModule\Web.config", @"Services\Support\OneModule")]
        public void ReadsOneModuleWebConfig()
        {
            WebConfigModuleInfoStore store = new WebConfigModuleInfoStore(@"Services\Support\OneModule");

            CustomModulesConfigurationSection section = store.GetModuleConfigurationSection();

            Assert.AreEqual(1, section.Modules.Count);
            Assert.AreEqual("Module1.Name", section.Modules[0].Name);
            Assert.AreEqual("Module1.AssemblyName", section.Modules[0].AssemblyName);
            Assert.AreEqual("Module1.VirtualPath", section.Modules[0].VirtualPath);
            Assert.AreEqual(0, section.Modules[0].Services.Count);
        }

        [TestMethod]
        [DeploymentItem(@"Services\Support\OneModuleWithOneService\Web.config", @"Services\Support\OneModuleWithOneService")]
        public void ReadsOneModuleWithOneServiceWebConfig()
        {
            WebConfigModuleInfoStore store = new WebConfigModuleInfoStore(@"Services\Support\OneModuleWithOneService");

            CustomModulesConfigurationSection section = store.GetModuleConfigurationSection();

            Assert.AreEqual(1, section.Modules.Count);
            Assert.AreEqual("Module1.Name", section.Modules[0].Name);
            Assert.AreEqual("Module1.AssemblyName", section.Modules[0].AssemblyName);
            Assert.AreEqual("Module1.VirtualPath", section.Modules[0].VirtualPath);
            Assert.AreEqual(1, section.Modules[0].Services.Count);
            Assert.AreEqual(typeof(IServiceLoader), section.Modules[0].Services[0].RegisterAs);
            Assert.AreEqual(typeof(ServiceLoader), section.Modules[0].Services[0].Type);
            Assert.AreEqual("Global", section.Modules[0].Services[0].Scope);
        }

        [TestMethod]
        [DeploymentItem(@"Services\Support\TwoModulesWithDependency\Web.config", @"Services\Support\TwoModulesWithDependency")]
        public void ReadsTwoModulesWithDependency()
        {
            WebConfigModuleInfoStore store = new WebConfigModuleInfoStore(@"Services\Support\TwoModulesWithDependency");

            CustomModulesConfigurationSection section = store.GetModuleConfigurationSection();

            Assert.AreEqual(2, section.Modules.Count);
            Assert.AreEqual("Module1.Name", section.Modules[0].Name);
            Assert.AreEqual("Module1.AssemblyName", section.Modules[0].AssemblyName);
            Assert.AreEqual("Module1.VirtualPath", section.Modules[0].VirtualPath);
            Assert.AreEqual("Module2.Name", section.Modules[0].Dependencies[0].Module);
            Assert.AreEqual("Module2.Name", section.Modules[1].Name);
            Assert.AreEqual("Module2.AssemblyName", section.Modules[1].AssemblyName);
            Assert.AreEqual("Module2.VirtualPath", section.Modules[1].VirtualPath);
        }

        [TestMethod]
        [DeploymentItem(@"Services\Support\TwoFiles\Web.config", @"Services\Support\TwoFiles")]
        [DeploymentItem(@"Services\Support\TwoFiles\SubFolder\Web.config", @"Services\Support\TwoFiles\SubFolder")]
        public void ReadsTwoModulesFromTwoFiles()
        {
            WebConfigModuleInfoStore store = new WebConfigModuleInfoStore(@"Services\Support\TwoFiles");

            CustomModulesConfigurationSection section = store.GetModuleConfigurationSection();

            Assert.AreEqual(2, section.Modules.Count);
            Assert.AreEqual("Module1.Name", section.Modules[0].Name);
            Assert.AreEqual("Module1.AssemblyName", section.Modules[0].AssemblyName);
            Assert.AreEqual("Module1.VirtualPath", section.Modules[0].VirtualPath);
            Assert.AreEqual("Module2.Name", section.Modules[1].Name);
            Assert.AreEqual("Module2.AssemblyName", section.Modules[1].AssemblyName);
            Assert.AreEqual("Module2.VirtualPath", section.Modules[1].VirtualPath);
        }

        [TestMethod]
        [DeploymentItem(@"Services\Support\TwoFilesWithServices\Web.config", @"Services\Support\TwoFilesWithServices")]
        [DeploymentItem(@"Services\Support\TwoFilesWithServices\SubFolder\Web.config", @"Services\Support\TwoFilesWithServices\SubFolder")]
        public void ReadsTwoModulesFromTwoFilesWithServices()
        {
            WebConfigModuleInfoStore store = new WebConfigModuleInfoStore(@"Services\Support\TwoFilesWithServices");

            CustomModulesConfigurationSection section = store.GetModuleConfigurationSection();

            Assert.AreEqual(2, section.Modules.Count);
            Assert.AreEqual("Module1.Name", section.Modules[0].Name);
            Assert.AreEqual("Module1.AssemblyName", section.Modules[0].AssemblyName);
            Assert.AreEqual("Module1.VirtualPath", section.Modules[0].VirtualPath);
            Assert.AreEqual("Module2.Name", section.Modules[1].Name);
            Assert.AreEqual("Module2.AssemblyName", section.Modules[1].AssemblyName);
            Assert.AreEqual("Module2.VirtualPath", section.Modules[1].VirtualPath);
            Assert.AreEqual(1, section.Modules[0].Services.Count);
            Assert.AreEqual(typeof(IServiceLoader), section.Modules[0].Services[0].RegisterAs);
            Assert.AreEqual(typeof(ServiceLoader), section.Modules[0].Services[0].Type);
            Assert.AreEqual("Global", section.Modules[0].Services[0].Scope);
            Assert.AreEqual(1, section.Modules[1].Services.Count);
            Assert.AreEqual(typeof(ICustomModuleInfoStore), section.Modules[1].Services[0].RegisterAs);
            Assert.AreEqual(typeof(WebConfigModuleInfoStore), section.Modules[1].Services[0].Type);
            Assert.AreEqual("Module", section.Modules[1].Services[0].Scope);
        }

        
        [TestMethod]
        [DeploymentItem(@"Services\Support\WebConfigWithRepeatedModules\Web.config", @"Services\Support\WebConfigWithRepeatedModules")]
        public void IfModuleIsLoadedDoesNotLoadItAgain()
        {
            WebConfigModuleInfoStore store = new WebConfigModuleInfoStore(@"Services\Support\WebConfigWithRepeatedModules");

            CustomModulesConfigurationSection section = store.GetModuleConfigurationSection();

            Assert.AreEqual(1, section.Modules.Count);
        }
    }
}
